package playn.game.java;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;

import playn.core.ResourceCallback;
import tiled.simple.core.Map;
import tiled.simple.reader.TMXReader;

public class MapsResourceCollector implements ResourceCallback<String>{
	private HashMap<String,Map> maps;
	private String currentKey;
	private ResourceChecker checker;

	
	public MapsResourceCollector(HashMap<String,Map> maps,String currentKey,ResourceChecker checker){
		this.maps=maps;
		this.currentKey=currentKey;
		this.checker=checker;
	}
	
	public void done(String resource) {
		System.out.println(currentKey+" loaded");

		TMXReader tmxReader=new TMXReader();
        InputStream is;
		try {
			is = new ByteArrayInputStream(resource.getBytes("UTF-8"));
			Map map=tmxReader.readMap(is);
			maps.put(currentKey, map);
			checker.CheckAllResources();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void error(Throwable err) {
		System.out.println(err.getMessage());
	}
	
}
